package com.aurora.oa.work.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.activity.PhotoShowActivity;
import com.aurora.oa.listener.OnRequestLogDetailListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.ToastUtil;
import com.aurora.oa.work.adapter.ImageSeeAdapter;
import com.aurora.oa.work.bean.AttachmentBean;
import com.aurora.oa.work.bean.WorkLogDataBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/21 0021.
 */
public class WorkLogDetailActivity extends BaseActivity {

    @Bind(R.id.sdv_head_img)
    SimpleDraweeView sdvHeadImg;
    @Bind(R.id.tv_title)
    TextView tvName;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_logtype)
    TextView tvLogtype;
    @Bind(R.id.tv_complete_work)
    TextView tvCompleteWork;
    @Bind(R.id.tv_uncomplete_work)
    TextView tvUncompleteWork;
    @Bind(R.id.tv_need_coordinate_work)
    TextView tvNeedCoordinateWork;
    @Bind(R.id.tv_complete_progress)
    TextView tvCompleteProgress;
    @Bind(R.id.tv_remark)
    TextView tvRemark;
    @Bind(R.id.gv_work_images)
    GridView gvWorkImages;

    private String id;
    private RequestHttpModel model = new RequestHttpModelImpl();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true, "日志详情", true);
        setMyContentView(R.layout.activity_work_log_detail);
        ButterKnife.bind(this);
        getData();
    }

    private void getData() {
        id =  getIntent().getStringExtra("id");
        model.getLogDetail(id,new OnRequestLogDetailListener(){

            @Override
            public void onRequestLogDetailSuccess(WorkLogDataBean logDataBean) {
                    initView(logDataBean);
            }

            @Override
            public void onRequestLogDetailFail(String message) {
                ToastUtil.show(getApplicationContext(),message);
            }
        });
    }

    private void initView(WorkLogDataBean logDataBean) {
        sdvHeadImg.setImageURI(AuroraConstants.BASE_SERVER_URL+"/"+logDataBean.getUserHeadImage());
       tvCompleteProgress.setText(logDataBean.getSchedule());
        tvCompleteWork.setText(logDataBean.getCompleted());
        // 1日报2周报3月报4年报
        String logType = "";
        switch (logDataBean.getType()){
            case 1:
                logType = "日报";
                break;
            case 2:
                logType = "周报";
                break;
            case 3:
                logType = "月报";
                break;
            case 4:
                logType = "年报";
                break;
            default:
                break;


        }

       tvLogtype.setText(logType);
        tvNeedCoordinateWork.setText(logDataBean.getCoordinated());
        tvRemark.setText(logDataBean.getRemark());
       tvUncompleteWork.setText(logDataBean.getUnfinished());
        tvName.setText(logDataBean.getUsername());
        tvTime.setText(logDataBean.getCreatedate());

      //把图片地址取出来用
        final List<String> uriList = new ArrayList<>();
        if(null!=logDataBean.getAttachments()){
            for(AttachmentBean e:logDataBean.getAttachments()){
                uriList.add(AuroraConstants.BASE_SERVER_URL+"/"+e.path);
            }
        }
       gvWorkImages.setAdapter(new ImageSeeAdapter(getApplicationContext(),uriList));
        gvWorkImages.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.setClass(WorkLogDetailActivity.this, PhotoShowActivity.class);
                intent.putStringArrayListExtra("list", (ArrayList<String>) uriList);
                startActivity(intent);
            }
        });
    }
}
